package mysql.cs.command;

import common.Util.ByteUtil;
import mysql.Commond;
import org.apache.commons.lang.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 *
 * @ClassName BinlogDumpCommand
 * @Description TODO
 * @Author fengk
 * @Date 2018/8/27 00:00
 */

public class BinlogDumpCommand extends Commond {
    public static final int BINLOG_SEND_ANNOTATE_ROWS_EVENT = 2;
    private long binlogPosition;
    private long slaveServerId;
    private String binlogFileName;
    public BinlogDumpCommand(){setCommand((byte)0x12);}
    @Override
    public void encode(byte[] data){
    }
    @Override
    public byte[] decode() throws IOException {
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        out.write(getCommand());
        ByteUtil.writeUnsignedIntLittleEndian(binlogPosition,out);
        int binlog_flags=0;
        binlog_flags|=BINLOG_SEND_ANNOTATE_ROWS_EVENT;
        out.write(binlog_flags);
        out.write(0x00);
        ByteUtil.writeUnsignedIntLittleEndian(slaveServerId,out);
        if(StringUtils.isNotEmpty(binlogFileName)){
            out.write(binlogFileName.getBytes());
        }
        return out.toByteArray();
    }

    public long getBinlogPosition() {
        return binlogPosition;
    }

    public void setBinlogPosition(long binlogPosition) {
        this.binlogPosition = binlogPosition;
    }

    public long getSlaveServerId() {
        return slaveServerId;
    }

    public void setSlaveServerId(long slaveServerId) {
        this.slaveServerId = slaveServerId;
    }

    public String getBinlogFileName() {
        return binlogFileName;
    }

    public void setBinlogFileName(String binlogFileName) {
        this.binlogFileName = binlogFileName;
    }
}
